package com.affectu;

import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;

import com.affectu.dao.DAOBase;
import com.affectu.task.Task;
import com.affectu.user.User;

/*
 * 2008-8-29
 * @author daniel nathan
 */
@Name("indexAction")
public class IndexAction extends DAOBase {

	@In(required = false, scope = ScopeType.SESSION)
	@Out(required = false, scope = ScopeType.SESSION)
	String searchString;

	@In(required = false, scope = ScopeType.SESSION)
	@Out(required = false, scope = ScopeType.SESSION)
	private String type;

	private String splitPattern = "[, ;，；]";

	private String url;

	private String skin;

	public String getSkin() {
		Object obj = this.getSessionScope().get("org.richfaces.SKIN");
		if (obj == null) {
			skin = "plain";
		} else {
			skin = obj.toString();
		}
		return skin;
	}

	public void setSkin(String skin) {
		this.getSessionScope().put("org.richfaces.SKIN", skin);
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@SuppressWarnings("unchecked")
	public String searchAction(String searchString, String type) {
		try {
			if ("task".equals(type)) {

				StringBuilder sb = new StringBuilder();// for the marked red
				sb.append("select t.id,");
				sb.append(this.markedRedAtkeyWordsInSQL(searchString, "name"));
				sb.append(this.markedRedAtkeyWordsInSQL(searchString, "tag"));
				// sb.append(this.markedRedAtkeyWordsInSQL(searchString,
				// "description"));
				sb.append("t.* ");
				String tables = "(select t1.* from Task t1 union select t2.* from OutTask t2) ";
				// ;
				sb
						.append("from "
								+ tables
								+ " t where concat(t.name,t.tag,t.description) regexp :searchString order by t.id desc");

				String regStr = searchString.replaceAll(this.splitPattern, "|");
				List<Task> tasks = em.createSQLQuery(sb.toString()).addEntity(
						"t", Task.class).setParameter("searchString", regStr)
						.list();
				this.getSessionScope().put("searchedTasks", tasks);
				this.getSessionScope().remove("searchedUsers");
			} else if ("wike".equals(type)) {
				List<User> users = em
						.createSQLQuery(
								"select u.* from User u where concat(u.nickName,ifnull(u.trueName,''),u.email) regexp :searchString")
						.addEntity("u", User.class).setParameter(
								"searchString", searchString).list();
				this.getSessionScope().put("searchedUsers", users);
				this.getSessionScope().remove("searchedTasks");
			}
			this.url = "/search/result.xhtml";

		} catch (Exception ex) {
			this.logException(ex);
		}
		return this.url;
	}

	public String markedRedAtkeyWordsInHtml(String inputString, int length) {
		String outString = this.filterHtmlTags(inputString, length);
		try {
			String[] strings = searchString.split(this.splitPattern);// split's
			for (int i = 0; i < strings.length; i++) {
				outString = outString.replaceAll(strings[i], this
						.addRedColor(strings[i]));
			}

		} catch (Exception ex) {
			this.logException(ex);
		}

		return outString;
	}

	public String markedRedAtkeyWordsInSQL(String keyWordsString,
			String columnName) {
		String statements = columnName;
		try {
			String[] strings = keyWordsString.split(this.splitPattern);// split's
			// creatirial
			for (int i = 0; i < strings.length; i++) {
				statements = "replace(" + statements + ",'" + strings[i]
						+ "','" + this.addRedColor(strings[i]) + "')";
			}
			statements += " '" + columnName + "', ";
		} catch (Exception ex) {
			this.logException(ex);
		}
		return statements;
	}

	public List<Task> searchResult() {

		return null;
	}
}
